package com.zhu.mqsms.task;

import com.tigerhhzz.service.SmsSenderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

/**
 *
 *
 * @param null
 * @return
 * @author zhuwanyi
 * @create 2024/12/11
 **/

@Component
public class SmsResendTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(SmsResendTask.class);

    private final SmsSenderService smsSenderService;
    private final JdbcTemplate jdbcTemplate;

    public SmsResendTask(SmsSenderService smsSenderService, JdbcTemplate jdbcTemplate) {
        this.smsSenderService = smsSenderService;
        this.jdbcTemplate = jdbcTemplate;
    }

    //@Scheduled(fixedRate = 60000) // 每分钟检查一次
    @Scheduled(cron = "0/30 * * * * ?")
    public void checkAndResendSms() {
        LOGGER.info("开始执行重新发送失败的消息！");
        jdbcTemplate.query("SELECT phone_number, unique_id FROM sms_log WHERE send_status = 0", (rs, rowNum) -> {
            String phoneNumber = rs.getString("phone_number");
            String uniqueId = rs.getString("unique_id");
            smsSenderService.sendSmsAsync(phoneNumber);
            return null;
        });
    }
}
